<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);

require APPPATH . 'core/feed/feed_bank.php';

class knowledge extends CI_Controller {

    protected $data;
    protected $news_cate_id;

    function __construct() {
        $this->_initdata();
    }

    //Controller: Trang chủ news
    function index() {
        $this->data['page_title'] = "Kiến thức XNK";
        $this->data['page_css'] = "css_knowledge";
        $this->data['keyword'] = "Nhập nội dung cần tìm";
        $this->data['page_name'] = "knowledge";
        $this->data['cate'] = $this->Fe_knowledge_model->get_all_cate();
        $this->data['listArticles'] = $this->showListArticles();

        $this->show($this->data);
    }

    function detail() {
        $currentPage = $this->uri->segment(2);
        $arrUrl = explode("-", $currentPage);
        $id = $arrUrl[sizeof($arrUrl) - 1];
        $result = $this->Fe_knowledge_model->get_article_by_id($id);

        $this->data['page_title'] = $result[0]->title;
        $this->data['page_css'] = "css_knowledge";
        $this->data['keyword'] = "Nhập nội dung cần tìm";
        $this->data['page_name'] = "knowledge_detail";

        $this->data['content'] = $result[0]->content;

        $this->show($this->data);
    }

    function showListArticles() {
        $currentPage = $this->uri->segment(3);
        if ($currentPage == "") {
            $currentPage = 1;
        }

        $cate = isset($_GET["cateId"]) ? $_GET["cateId"] : "0";
        $title = isset($_GET["txtSearch"]) ? $_GET["txtSearch"] : "";
        $this->data['currentPage'] = $currentPage;

        list($count, $listArticles) = $this->Fe_knowledge_model->get_list_articles($title, $cate, 10, ($currentPage - 1) * 10);

        $this->load->library('Mobile_Detect');
        $detect = new Mobile_Detect();
        if ($detect->isMobile() || $detect->isTablet() || $detect->isAndroidOS())
            $this->data['paging'] = $this->paging_mobile($count, $currentPage, 10);
        else
            $this->data['paging'] = $this->paging($count, $currentPage, 10);

        return $listArticles;
    }

    function paging($count, $curPage, $pageSize) {

        $numberPage = ceil($count / $pageSize);

        $startPage = 1;
        $endPage = $numberPage;
        $prev = $curPage > 1 ? $curPage - 1 : $curPage;
        $next = $curPage < $numberPage ? $curPage + 1 : $numberPage;

        if ($endPage > 5) {
            $startPage = $curPage - 2 < 1 ? 1 : $curPage - 2;
            $endPage = $curPage + 2 > $numberPage ? $numberPage : $curPage + 2;
        }

        if (isset($_GET["txtSearch"])) {
            $html = '<a href="' . $this->config->item('base_url') . 'kien-thuc-xnk/tim-kiem/' . $prev . '?txtSearch=' . $_GET["txtSearch"] . '&cateId=' . $_GET["cateId"] . '" class="paging-previous_next"><img src="http://eximgate.vn/style/images/img20.gif" border="0"></a>';
            for ($i = $startPage; $i <= $endPage; $i++) {
                if ($curPage == $i) {
                    $html .= '<a href="' . $this->config->item('base_url') . 'kien-thuc-xnk/tim-kiem/' . $i . '?txtSearch=' . $_GET["txtSearch"] . '&cateId=' . $_GET["cateId"] . '" class="paging-item-selected">' . $i . '</a>';
                } else {
                    $html .= '<a href="' . $this->config->item('base_url') . 'kien-thuc-xnk/tim-kiem/' . $i . '?txtSearch=' . $_GET["txtSearch"] . '&cateId=' . $_GET["cateId"] . '" class="paging-item">' . $i . '</a>';
                }
            }
            $html .= '<a href="' . $this->config->item('base_url') . 'kien-thuc-xnk/tim-kiem/' . $next . '?txtSearch=' . $_GET["txtSearch"] . '&cateId=' . $_GET["cateId"] . '" class="paging-previous_next"><img src="http://eximgate.vn/style/images/img21.gif" border="0"></a>';
        } else {
            $html = '<a href="' . $this->config->item('base_url') . 'kien-thuc-xnk/page/' . $prev . '" class="paging-previous_next"><img src="http://eximgate.vn/style/images/img20.gif" border="0"></a>';
            for ($i = $startPage; $i <= $endPage; $i++) {
                if ($curPage == $i) {
                    $html .= '<a href="' . $this->config->item('base_url') . 'kien-thuc-xnk/page/' . $i . '" class="paging-item-selected">' . $i . '</a>';
                } else {
                    $html .= '<a href="' . $this->config->item('base_url') . 'kien-thuc-xnk/page/' . $i . '" class="paging-item">' . $i . '</a>';
                }
            }
            $html .= '<a href="' . $this->config->item('base_url') . 'kien-thuc-xnk/' . $next . '" class="paging-previous_next"><img src="http://eximgate.vn/style/images/img21.gif" border="0"></a>';
        }

        return $html;
    }

    function paging_mobile($count, $curPage, $pageSize) {

        $numberPage = ceil($count / $pageSize);

        $startPage = 1;
        $endPage = $numberPage;
        $prev = $curPage > 1 ? $curPage - 1 : $curPage;
        $next = $curPage < $numberPage ? $curPage + 1 : $numberPage;

        if ($endPage > 5) {
            $startPage = $curPage - 2 < 1 ? 1 : $curPage - 2;
            $endPage = $curPage + 2 > $numberPage ? $numberPage : $curPage + 2;
        }

        $html = '<div class="center"><nav><ul class="pagination">';

        if (isset($_GET["txtSearch"])) {
            $html .= '<li class="disabled"><a href="' . $this->config->item('base_url') . 'kien-thuc-xnk/tim-kiem/' . $prev . '?txtSearch=' . $_GET["txtSearch"] . '&cateId=' . $_GET["cateId"] . '">&laquo;</a></li>';
            for ($i = $startPage; $i <= $endPage; $i++) {
                if ($curPage == $i) {
                    $html .= '<li class="active"><a href="' . $this->config->item('base_url') . 'kien-thuc-xnk/tim-kiem/' . $i . '?txtSearch=' . $_GET["txtSearch"] . '&cateId=' . $_GET["cateId"] . '">' . $i . '<span class="sr-only">(current)</span></a></li>';
                } else {
                    $html .= '<li class="disabled"><a href="' . $this->config->item('base_url') . 'kien-thuc-xnk/tim-kiem/' . $i . '?txtSearch=' . $_GET["txtSearch"] . '&cateId=' . $_GET["cateId"] . '">' . $i . '</a></li>';
                }
            }
            $html .= '<li class="disabled"><a href="' . $this->config->item('base_url') . 'kien-thuc-xnk/tim-kiem/' . $next . '?txtSearch=' . $_GET["txtSearch"] . '&cateId=' . $_GET["cateId"] . '">&raquo;</a></li>';
        } else {
            $html .= '<li class="disabled"><a href="' . $this->config->item('base_url') . 'kien-thuc-xnk/page/' . $prev . '">&laquo;</a></li>';
            for ($i = $startPage; $i <= $endPage; $i++) {
                if ($curPage == $i) {
                    $html .= '<li class="active"><a href="' . $this->config->item('base_url') . 'kien-thuc-xnk/page/' . $i . '">' . $i . '<span class="sr-only">(current)</span></a></li>';
                } else {
                    $html .= '<li class="disabled"><a href="' . $this->config->item('base_url') . 'kien-thuc-xnk/page/' . $i . '">' . $i . '</a></li>';
                }
            }
            $html .= '<li class="disabled"><a href="' . $this->config->item('base_url') . 'kien-thuc-xnk/page/' . $next . '">&raquo;</a></li>';
        }

        $html .= '</ul></nav></div>';

        return $html;
    }

    function _initdata() {
        parent::__construct();
        session_start();
        $this->load->helper('eximcached');
        
        $this->load->model('Frontendmodel');
        $this->load->model('Fe_adv_model');
        $this->load->model('Fe_company_model');
        $this->load->model('Fe_docs_model');
        $this->load->model('Fe_forwarding_model');
        $this->load->model('Fe_knowledge_model');
        $this->load->model('Fe_faq_model');
        
        //load submenu and footer
        list($this->data['aLatestNews']) = get_latest_news($this->Frontendmodel, 7);
        list($this->data['aCate2']) = get_newscate_list(NEWS_CATE_VBPQ, $this->Frontendmodel);
        $this->data['aCate3'] = $this->Fe_knowledge_model->get_all_cate();
        $this->data['aCate4'] = _get_all_cate_customs($this->Frontendmodel);
        $this->data['aCate5'] = $this->Fe_forwarding_model->get_all_cate();
        $this->data['aCate6'] = $this->Fe_faq_model->_get_faq_cate_list();

        $this->data['page_header'] = "header";
        $this->data['page_rightbar'] = "rightbar";
        $this->data['page_footer'] = "footer";
        $this->data['base_url'] = $this->config->item('base_url');
        $this->data['img_url'] = $this->config->item('img_url');
        $this->data['current_url'] = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
        $this->data['aCityList'] = get_cities_list($this->Frontendmodel);
        $feed = new feed_bank();
        $this->data['gold'] = get_feed_gold($feed);
        $this->data['exchange_rate'] = get_feed_exchange($feed);
    }

}

?>